package com.tt.lcd.qunyangGPRS.systemActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.tt.lcd.qunyangGPRS.R;
import com.tt.lcd.qunyangGPRS.dao.MyUser;
import com.tt.lcd.qunyangGPRS.utils.Register;

public class UserActivity extends Activity {
	private TextView ok;
	private EditText name;

	IntentFilter intf;

	private BroadcastReceiver br = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getStringExtra("key2").equals("OK")) {

				finish();
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_reight);

		intf = new IntentFilter("com.lcd.qyzhuceRec");// 参数过滤器筛选条件，可以理解为广播名称
		registerReceiver(br, intf); // 注册广播


		ok = (TextView) findViewById(R.id.user_bt_ok);
		name = (EditText) findViewById(R.id.user_et_name);

		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//如果输入为空不执行
				if(name.getText().toString().trim().equals(""))return;
				//如果已经有用户，不执行
				if(!new MyUser(UserActivity.this).selectUID().equals(""))return;
				//保存用户名
				new MyUser(UserActivity.this).insert(name.getText().toString().trim());
				//执行注册操作
				Register.getRegister().server.registUser(name.getText().toString().trim());
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(br);
		super.onDestroy();
	}

}
